package core;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import core.*;


public class GameServerConfig {

    private int serverPort;
    private String dburl;
    private String dbname;
    private String dbuser;
    private String dbpassword;
    //man- private String bugServerPath;

    /**
     * Main method, that reads the configuration file and extracts all the necessary information.
     */
    public void configure() {
        try {
            // Open file, prepare for reading.
            //man-
            FileReader configFileReader = new FileReader("../conf/game_server_config.txt");
            BufferedReader bufferedConfigFileReader = new BufferedReader(configFileReader);

            // Read the configuration file one line at the time,
            // until end of the file is reached,
            // extract configuration data.
            String configFileLine;
            while ((configFileLine = bufferedConfigFileReader.readLine()) != null) {
                // If line starts with '#' character,
                // it is a comment line and should be skipped.
                if (!configFileLine.isEmpty() && !configFileLine.trim().startsWith("#")) {
                    // If not a comment, extract configuration information.

                    // Create instance of StringTokenizer for the line.
                    StringTokenizer stok = new StringTokenizer(configFileLine);
                    // Go through the elements of the line, identify and save information.
                    while (stok.hasMoreTokens()) {
                        String next = stok.nextToken();
                        String parameterValue = "";

                        if (next.equalsIgnoreCase("ServerPort")) {
                            parameterValue = stok.nextToken();
                            setServerPort(Integer.parseInt(parameterValue));
                        }

                        if (next.equalsIgnoreCase("DBUrl")) {
                            parameterValue = stok.nextToken();
                            setDBUrl(parameterValue);
                        }

                        if (next.equalsIgnoreCase("DBName")) {
                            parameterValue = stok.nextToken();
                            setDBName(parameterValue);
                        }

                        if (next.equalsIgnoreCase("DBUser")) {
                            parameterValue = stok.nextToken();
                            setDBUser(parameterValue);
                        }

                        if (next.equalsIgnoreCase("DBPassword")) {
                            parameterValue = stok.nextToken();
                            setDBPassword(parameterValue);
                        }

                        if (next.equalsIgnoreCase("BugServerPath")) {
                            stok.nextToken("\"");
                            parameterValue = stok.nextToken("\"");
                            //man- setBugServerPath(parameterValue);
                        }
                    }
                }
            }
        } catch (IOException e) {
            System.err.println("Unable to read configuration file"+e.toString());
            System.exit(-1);
        }
    }

    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }

    public int getServerPort() {
        return serverPort;
    }

    public void setDBUrl(String dburl) {
        this.dburl = dburl;
    }

    public String getDBUrl() {
        return dburl;
    }

    public void setDBName(String dbname) {
        this.dbname = dbname;
    }

    public String getDBName() {
        return dbname;
    }

    public void setDBUser(String dbuser) {
        this.dbuser = dbuser;
    }

    public String getDBUser() {
        return dbuser;
    }

    public void setDBPassword(String dbpassword) {
        this.dbpassword = dbpassword;
    }

    public String getDBPassword() {
        return dbpassword;
    }
    /*man-
    public void setBugServerPath(String path) {
        bugServerPath = path;
    }

    public String getBugServerPath() {
        return bugServerPath;
    }*/
}
